<template>
  <div class="AddOrUpdate_main add_container column_one">
    <div class="main_main">
      <el-form ref="form" :model="formData" :rules="rules" :inline="true">
        <el-form-item label="菜单名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item label="公司名称" prop="customerIds">
          <el-select v-model="formData.customerIds" filterable multiple placeholder="请选择" clearable collapse-tags>
            <el-option
              v-for="item in companyOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
              clearable
            />
          </el-select>
        </el-form-item>
        <el-form-item prop="path">
          <span slot="label">
            <el-tooltip content="访问的路由地址，如：`user`" placement="top">
              <i class="el-icon-question" />
            </el-tooltip>
            路由地址
          </span>
          <el-input v-model="formData.path" placeholder="请输入" clearable />
        </el-form-item>
        <el-form-item prop="component">
          <span slot="label">
            <el-tooltip content="访问的组件路径，如：`system/user/index`，默认在`views`目录下" placement="top">
              <i class="el-icon-question" />
            </el-tooltip>
            组件路径
          </span>
          <el-input v-model="formData.component" placeholder="请输入" clearable />
        </el-form-item>
      </el-form>
    </div>
    <div class="main_bottom">
      <el-button type="cancel" @click.stop="cancel">取消</el-button>
      <el-button @click.stop="onSubmit">保存</el-button>
    </div>
  </div>
</template>

<script>
import { addOrUpdate, getInfo, companylist } from '@/api/superManage/customizationServeConfiguration'
export default {
  name: 'AddOrUpdate',
  props: {
    layerid: {},
    id: {},
    defaultValue: {}
  },
  data() {
    return {
      formData: {
        id: undefined,
        name: undefined,
        menuType: undefined,
        path: undefined,
        component: undefined,
        customerIds: []
      },
      companyOptions: [],
      rules: {
        name: [
          { required: true, message: '菜单名称不能为空', trigger: 'change' }
        ],
        component: [
          { required: true, message: '组件路径不能为空', trigger: 'change' }
        ],
        path: [
          { required: true, message: '路由地址不能为空', trigger: 'change' }
        ],
        customerIds: [
          { required: true, message: '公司名称不能为空', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.getCompanyList()
    if (!this.id) { // 没有id就是新增
      Object.keys(this.defaultValue).forEach(key => { // 给右边数据赋值
        this.formData[key] = this.defaultValue[key]
      })
    } else { // 有id就是编辑
      this.getInfo()
    }
  },
  methods: {
    getCompanyList() { // 获取公司信息
      companylist().then(res => {
        this.companyOptions = res.data
      })
    },
    getInfo() {
      getInfo(this.id).then(res => {
        Object.keys(this.formData).forEach(key => { // 给右边数据赋值
          this.formData[key] = res.data[key]
        })
        this.formData.customerIds = this.formData.customerIds.split(',').map(Number) // 多选巡检人员字符串转数组再把ID转成数字型
      })
    },
    onSubmit() {
      const customerIds = this.formData.customerIds.toString()
      this.formData.customerIds = customerIds // 转换成字符串
      this.$refs['form'].validate(valid => {
        if (valid) {
          addOrUpdate(this.formData).then(res => {
            this.$message({
              message: res.msg,
              type: 'success'
            })
            this.$layer.close(this.layerid) // 关闭弹框
            this.$parent.refresh() // 调用父组件的刷新方法
          })
        }
      })
    },
    cancel() {
      this.$layer.close(this.layerid)// 关闭弹框
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .el-select__tags {
  max-width: 100% !important;
}
</style>
